<?php

// załączamy plik z handlerem tworzącym nowy rodzaj treści
require_once('classes/handlers/GcCreateNodeTypeHandler.class.php');


function groups_catalog_admin_settings() {
  
  $form['groups_content_cols_per_row'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of cols in a row'),
    '#description' => t('Enter the maximum number of content columns in a row'),
    '#default_value' => variable_get('groups_content_cols_per_row', 4),
    '#size' => 2
  );
  $form['groups_content_rows_per_page'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of rows per page'),
    '#description' => t('Enter the maximum number of content rows per page'),
    '#default_value' => variable_get('groups_content_rows_per_page', 4),
    '#size' => 2
  );
  
  return system_settings_form($form);
}


/**
 * Display a list of classes.
 */
function groups_catalog_classes() {
  $result = db_query("SELECT * FROM {gc_classes}");
  $header = array(t('Class ID'), t('Name'), t('Description'), t('Operations'));
  $rows = array();
  while ($class = db_fetch_object($result)) {
    $ops = array(
      l(t('delete'), 'admin/gc/classes/'. $class->pcid .'/delete'),
    );
    $rows[] = array(
      $class->pcid,
      $class->name,
      $class->description,
      implode(' ', $ops),
    );
  }
  if (count($rows) == 0) {
    $rows[] = array(array('data' => t('No classes have been defined yet.'), 'colspan' => '5'));
  }

  $output = theme('table', $header, $rows);
  $output .= '<h2>'. t('Add a class') .'</h2>';
  $output .= drupal_get_form('groups_catalog_class_form');

  return $output;
}

function groups_catalog_class_form($form_state, $class = NULL) {
  if (!is_null($class)) {
    $classname = $class->name;
    $classdesc = $class->description;
    drupal_set_title(check_plain($classname));
    $form['pcid'] = array(
      '#type' => 'hidden',
      '#value' => $class->pcid
    );
  }
  else {
    $classname = '';
    $classdesc = '';
    $form['pcid'] = array(
      '#type' => 'textfield',
      '#title' => t('Class ID'),
      '#required' => TRUE,
      '#max_length' => 32,
      '#description' => t('The machine-readable name of this content type. This text will be used for constructing the URL of the <em>create content</em> page for this content type. This name may consist only of lowercase letters, numbers, and underscores. Dashes are not allowed. Underscores will be converted into dashes when constructing the URL of the <em>create content</em> page. This name must be unique to this content type.'),
    );
  }
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Class name'),
    '#default_value' => $classname,
    '#required' => TRUE,
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#description' => t('This text describes the content type created for this product class to administrators.'),
    '#default_value' => $classdesc,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}

/**
 * Ensure the new product class is unique.
 *
 * @see uc_product_class_form()
 */
function groups_catalog_class_form_validate($form, &$form_state) {
  if ($form['pcid']['#type'] == 'textfield') {
    $type = node_get_types('type', $form_state['values']['pcid']);
    if ($type) {
      if ($type->module == 'catalog_group') {
        form_set_error('pcid', t('This class already exists.'));
      }
      elseif ($type->custom == 0) {
        form_set_error('pcid', t('This is a node type provided by another module. Only custom node types may become product classes.'));
      }
    }
  }
}

/**
 * @see uc_product_class_form()
 */
function groups_catalog_class_form_submit($form, &$form_state) {
  $is_new = $form['pcid']['#type'] == 'textfield';

  //id nowego typu treści
  $pcid = $form_state['values']['pcid'];
  
  // nazwa dla użytkownika
  $name = $form_state['values']['name'];

  if ($is_new) {
    /* TODO
     * stworzyć procedurę dodawania nowego typu zawartości
     */
  	$pcid = preg_replace(array('/\s+/', '/\W/'), array('_', ''), strtolower($pcid));
  	$gcCreateNodeTypeHandler = new GcCreateNodeTypeHandler($pcid, $name);
  	$gcCreateNodeTypeHandler->handle();
  	
  }

  node_types_rebuild();
  _block_rehash();
  menu_rebuild();

  drupal_set_message(t('Class saved.'));
}

/**
 * Confirm the deletion of a product class.
 *
 * @see uc_product_class_delete_confirm_submit()
 */
function gc_class_delete_confirm($form_state, $class) {
  $form = array();

  $form['pcid'] = array(
    '#type' => 'value',
    '#value' => $class->pcid,
  );

  $question = t('Are you sure you want to delete the %type class?', array('%type' => $class->pcid));
  $description = t('The node type associated with this class will revert to a standard node type.');

  // Find out how many nodes of this class exist and add to the description.
  $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = '%s'", $class->pcid));

  if ($count > 0) {
    $description .= '<br />'. format_plural($count, 'There is @count node of this type.', 'There are @count nodes of this type.');
  }

  return confirm_form($form, $question, 'admin/gc/classes', $description, t('Delete'), t('Cancel'));
}

/**
 * @see uc_product_class_delete_confirm()
 */
function gc_class_delete_confirm_submit($form, &$form_state) {
  $type = node_get_types('type', $form_state['values']['pcid']);
  node_type_delete($type);

  db_query("DELETE FROM {gc_classes} WHERE pcid = '%s'", $form_state['values']['pcid']);
  node_types_rebuild();
  menu_rebuild();
  _block_rehash();
  
  drupal_set_message(t('class %type deleted.', array('%type' => $form_state['values']['pcid'])));

  $form_state['redirect'] = 'admin/settings/groups_catalog/classes';
}
